﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MAT.Model;
using System.Windows.Forms;

namespace MAT.Controllers
{
    class ctrMain
    {
        public static frmMain _frmMain = null;
        Controllers.ctrNotice _ctrNotice = new Controllers.ctrNotice();

        public frmMain FrmMain
        {
            get { return _frmMain; }
            set { _frmMain = value; }
        }

        MAT_Context context = MATContext.getContext();

        private void priority_admin0()
        {
            _frmMain.toolStripSplitButton_UserName.Text = MAT.Controllers.ctrLogin.Username + " (admin0)";
            _frmMain.manageToolStripMenuItem.Enabled = true;
            _frmMain.manageUsersToolStripMenuItem.Enabled = true;
            _frmMain.requestAssetLiquidationToolStripMenuItem.Enabled = true;
            _frmMain.findToolStripMenuItem.Enabled = true;
            _frmMain.reportsToolStripMenuItem.Enabled = true;
            _frmMain.moveToolStripMenuItem.Enabled = true;
            _frmMain.manageEmployeeToolStripMenuItem.Enabled = true;
            _frmMain.addItemsToolStripMenuItem.Enabled = true;
        }
        private void priority_admin1()
        {
            _frmMain.toolStripSplitButton_UserName.Text = MAT.Controllers.ctrLogin.Username + " (admin_1)";
            _frmMain.manageToolStripMenuItem.Enabled = true;
            _frmMain.manageUsersToolStripMenuItem.Enabled = false;
            _frmMain.requestAssetLiquidationToolStripMenuItem.Enabled = true;
            _frmMain.findToolStripMenuItem.Enabled = true;
            _frmMain.reportsToolStripMenuItem.Enabled = true;
            _frmMain.moveToolStripMenuItem.Enabled = true;
            _frmMain.manageEmployeeToolStripMenuItem.Enabled = true;
            _frmMain.addItemsToolStripMenuItem.Enabled = true;
        }
        private void priority_admin2()
        {
            _frmMain.toolStripSplitButton_UserName.Text = MAT.Controllers.ctrLogin.Username + " (admin_2)";
            _frmMain.manageToolStripMenuItem.Enabled = false;
            _frmMain.requestAssetLiquidationToolStripMenuItem.Enabled = false;
            _frmMain.findToolStripMenuItem.Enabled = false;
            _frmMain.reportsToolStripMenuItem.Enabled = false;
            _frmMain.moveToolStripMenuItem.Enabled = false;
            _frmMain.manageEmployeeToolStripMenuItem.Enabled = false;
            _frmMain.addItemsToolStripMenuItem.Enabled = false;
        }
        private void priority_user()
        {
            _frmMain.toolStripSplitButton_UserName.Text = MAT.Controllers.ctrLogin.Username + " (user)";
            _frmMain.manageToolStripMenuItem.Enabled = false;
            _frmMain.requestAssetLiquidationToolStripMenuItem.Enabled = false;
            _frmMain.findToolStripMenuItem.Enabled = false;
            _frmMain.reportsToolStripMenuItem.Enabled = false;
            _frmMain.moveToolStripMenuItem.Enabled = false;
            _frmMain.manageEmployeeToolStripMenuItem.Enabled = false;
            _frmMain.addItemsToolStripMenuItem.Enabled = false;
        }

        private void start_function(int rip)
        {
            switch (rip)
            {
                case 11: manageUsersToolStripMenuItem_Click(); break;
                case 21: viewAssetProfileToolStripMenuItem_Click(); break;
                case 22: addItemsToolStripMenuItem_Click(); break;
                case 23: moveToolStripMenuItem_Click(); break;
                case 24: findToolStripMenuItem_Click(); break;
                case 31: manageSupplierToolStripMenuItem_Click(); break;
                case 32: manageCategoryToolStripMenuItem_Click(); break;
                case 33: manageRoomToolStripMenuItem_Click(); break;
                case 34: manageEmployeeToolStripMenuItem_Click(); break;
                case 41: reportAssetByRoomToolStripMenuItem_Click(); break;
                case 42: reportAssetByCategoryToolStripMenuItem_Click(); break;
                case 43: reportAssetBySupplierToolStripMenuItem_Click(); break;
                case 44: reportAssetByLiquidationToolStripMenuItem_Click(); break;
                case 45: reportAssetByMaintenanceToolStripMenuItem_Click(); break;
                case 46: reportSupplierListToolStripMenuItem_Click(); break;
                case 47: reportTotalAssetValueToolStripMenuItem_Click(); break;
                case 48: reportMovingAssetToolStripMenuItem_Click(); break;
                case 51: requestAssetLiquidationToolStripMenuItem_Click(); break;
                case 52: requestAssetMaintenanceToolStripMenuItem_Click(); break;
                case 53: requestBorrowAssetToolStripMenuItem_Click(); break;
                case 54: requestNewAssetToolStripMenuItem_Click(); break;
            }
            rip = 0;
        }

        //Set location and size of panel
        private void setPanel(int w, int h, Panel p)
        {
            int resultX = (_frmMain.Width - w) / 2; 
            int resultY = (_frmMain.Height - h) / 2;
            p.Location = new System.Drawing.Point(resultX, resultY);
            p.Width = w;
            p.Height = h;
        }

        public void frmMain_Load()
        {
            if (MAT.Controllers.ctrNotice._frmNotice == null)
            {
                MAT.Controllers.ctrNotice._frmNotice = new frmNotice();
                MAT.Controllers.ctrNotice._frmNotice.timerLoad.Start();
                MAT.Controllers.ctrNotice._frmNotice.MAT_NotifyIcon.ShowBalloonTip(3000, "Information", "Notice form is actived!", ToolTipIcon.Info);
                MAT.Controllers.ctrNotice._frmNotice.Hide();
                //frmNotice fNotice = new frmNotice();
                //fNotice.timerLoad.Start();
                //fNotice.MAT_NotifyIcon.ShowBalloonTip(3000, "Information", "Notice form is actived!", ToolTipIcon.Info);
                //fNotice.Hide();
            }
            _ctrNotice.load_check_data();
            MAT.Controllers.ctrLogin.writeFile(MAT.Controllers.ctrLogin.Username, MAT.Controllers.ctrLogin.priority, MAT.Controllers.ctrLogin.employee_ID);

            _frmMain.toolStripSplitButton_AccessDate.Text = DateTime.Today.ToLongDateString();
            switch (MAT.Controllers.ctrLogin.priority)
            {
                case 0: priority_admin0(); break;
                case 1: priority_admin1(); break;
                case 2: priority_admin2(); break;
                case 3: priority_user(); break;
            }
            if (MAT.Controllers.ctrOption.rip != 0)
                start_function(MAT.Controllers.ctrOption.rip);
        }

        public void btnSignOut_Click()
        {
            if (MessageBox.Show("Do you really want to sign out?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    //Set flag for user (logged in)
                    LOG_IN login = context.LOG_IN.FirstOrDefault(log => log.USERNAME == MAT.Controllers.ctrLogin.Username);
                    login.LOGGED = 0;
                    context.SaveChanges();

                    _frmMain.Dispose();
                    MAT.Controllers.ctrNotice._frmNotice.Dispose();
                    MAT.Controllers.ctrOption._frmOption.Dispose();
                    frmLogin f = new frmLogin();
                    f.ShowDialog();
                    f.Focus();
                }
                catch { }
            }
        }

        public void btnMinimize_Click()
        {
            _frmMain.WindowState = FormWindowState.Minimized;
        }

        public void btnClose_Click()
        {
            _frmMain.Hide();
        }

        //USER ACCOUNT Function
        public void changePasswordToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Controls.Clear();
            frmChangePassword fCP = new frmChangePassword();
            fCP.ShowDialog();
        }

        public void manageUsersToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Controls.Clear();
            frmManageUser fMR = new frmManageUser();
            setPanel(fMR.Width, fMR.Height, _frmMain.pnlMain);
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        //ACTIONS function
        public void viewAssetProfileToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Controls.Clear();
            _frmMain.pnlMain.Dock = DockStyle.Fill;
            frmAssetProfile fMR = new frmAssetProfile();
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        public void addItemsToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Controls.Clear();
            frmAddItem fAI = new frmAddItem();
            fAI.Show();
        }

        public void moveToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Controls.Clear();
            frmMoveAsset fMR = new frmMoveAsset();
            setPanel(fMR.Width, fMR.Height, _frmMain.pnlMain);
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        public void findToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Controls.Clear();
            _frmMain.pnlMain.Dock = DockStyle.Fill;
            frmFindAsset fMR = new frmFindAsset();
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        //MANAGE function
        public void manageSupplierToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Dock = DockStyle.Fill;
            _frmMain.pnlMain.Controls.Clear();
            frmManageSupplier fMR = new frmManageSupplier();
            setPanel(fMR.Width, fMR.Height, _frmMain.pnlMain);
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        public void manageCategoryToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Dock = DockStyle.Fill;
            _frmMain.pnlMain.Controls.Clear();
            frmManageCategory fMR = new frmManageCategory();
            setPanel(fMR.Width, fMR.Height, _frmMain.pnlMain);
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        public void manageRoomToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Dock = DockStyle.Fill;
            _frmMain.pnlMain.Controls.Clear();
            frmManageRoom fMR = new frmManageRoom();
            setPanel(fMR.Width, fMR.Height, _frmMain.pnlMain);
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.None;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        public void manageEmployeeToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Dock = DockStyle.Fill;
            _frmMain.pnlMain.Controls.Clear();
            frmManageEmployee fMR = new frmManageEmployee();
            setPanel(fMR.Width, fMR.Height, _frmMain.pnlMain);
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        //REPORTS function
        public void reportAssetByRoomToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Controls.Clear();
            _frmMain.pnlMain.Dock = DockStyle.Fill;
            frmReportRoom fMR = new frmReportRoom();
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        public void reportAssetByCategoryToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Controls.Clear();
            _frmMain.pnlMain.Dock = DockStyle.Fill;
            frmReportCategory fMR = new frmReportCategory();
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        public void reportAssetBySupplierToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Controls.Clear();
            _frmMain.pnlMain.Dock = DockStyle.Fill;
            frmReportSupplier fMR = new frmReportSupplier();
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        public void reportAssetByLiquidationToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Controls.Clear();
            _frmMain.pnlMain.Dock = DockStyle.Fill;
            frmReportAssetLiquidation fMR = new frmReportAssetLiquidation();
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        public void reportAssetByMaintenanceToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Controls.Clear();
            _frmMain.pnlMain.Dock = DockStyle.Fill;
            frmReportAssetMaintenance fMR = new frmReportAssetMaintenance();
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        public void reportSupplierListToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Controls.Clear();
            _frmMain.pnlMain.Dock = DockStyle.Fill;
            frmSupplierReport fMR = new frmSupplierReport();
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        public void reportTotalAssetValueToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Controls.Clear();
            _frmMain.pnlMain.Dock = DockStyle.Fill;
            frmTotalAssetValue fMR = new frmTotalAssetValue();
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        public void reportMovingAssetToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Controls.Clear();
            _frmMain.pnlMain.Dock = DockStyle.Fill;
            frmMoveAssetReport fMR = new frmMoveAssetReport();
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        //SEND REQUESTS function
        public void requestAssetLiquidationToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Dock = DockStyle.Fill;
            _frmMain.pnlMain.Controls.Clear();
            frmRequestAssetLiquidation fMR = new frmRequestAssetLiquidation();
            setPanel(fMR.Width, fMR.Height, _frmMain.pnlMain);
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        public void requestAssetMaintenanceToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Dock = DockStyle.None;
            _frmMain.pnlMain.Controls.Clear();
            frmRequestAssetMaintenance fMR = new frmRequestAssetMaintenance();
            setPanel(fMR.Width, fMR.Height, _frmMain.pnlMain);
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        public void requestBorrowAssetToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Dock = DockStyle.None;
            _frmMain.pnlMain.Controls.Clear();
            frmRequestBorrowAsset fMR = new frmRequestBorrowAsset();
            setPanel(fMR.Width, fMR.Height, _frmMain.pnlMain);
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        public void requestNewAssetToolStripMenuItem_Click()
        {
            _frmMain.pnlMain.Visible = true;
            _frmMain.pnlMain.Dock = DockStyle.None;
            _frmMain.pnlMain.Controls.Clear();
            frmRequestNewAsset fMR = new frmRequestNewAsset();
            setPanel(fMR.Width, fMR.Height, _frmMain.pnlMain);
            fMR.TopLevel = false;
            fMR.FormBorderStyle = FormBorderStyle.None;
            fMR.Dock = DockStyle.Fill;
            fMR.StartPosition = FormStartPosition.CenterParent;
            _frmMain.pnlMain.Controls.Add(fMR);
            fMR.Show();
        }

        //ABOUT US function
        public void aboutMATToolStripMenuItem_Click()
        {
            MessageBox.Show("Mobile Asset Tracking - MAT \n"
                            + "Jupiter Team \n\n"
                            + "PHAM T.L. Trinh \n"
                            + "HO T. Duc \n"
                            + "NGUYEN H. Phuc \n"
                            + "NGUYEN T. Anh \n"
                            + "NGUYEN T.H. Thanh");
        }

        public void contentToolStripMenuItem_Click()
        {
            System.Diagnostics.Process.Start(Application.StartupPath + @"/../../MAT-DOC-User Manual-V3.0.pdf");
        }
    }
}
